package com.notepad.ink;

import android.content.ContentValues;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import com.note.provider.Ink.Inks;

public class InksDAO {
	// ------------------------------------------------------------ Constants
	private static final String[] INK_PROJECTION = new String[] {
		Inks._ID, Inks.BATCH, Inks.PAGE_NUMBER, Inks.CONTENT, Inks.IMAGE_BUFFER, Inks.TEXT_BUFFER,
		Inks.BATCH_COUNT
	};
	
	// ------------------------------------------------------------ Public methods
	/**
	 * ѡ���ѯ��
	 * @param context context��
	 * @param key ������
	 * @return ���
	 */
	public static InksValue selectSelective(Context context, InksValue key) {
		Cursor cursor = context.getContentResolver().query(Inks.CONTENT_URI, INK_PROJECTION, 
				getWhereClause(key), null, null);
		
		InksValue value = null;
		if (cursor.moveToFirst())
			value = getData(cursor);
		cursor.close();
		
		return value;
	}
	
	/**
	 * ��ѯ������
	 * @param context context��
	 * @param key ������
	 * @return ������
	 */
	public static int selectCount(Context context, InksValue key) {
		Cursor cursor = context.getContentResolver().query(Inks.CONTENT_URI, new String[] {Inks._COUNT}, 
				getWhereClause(key), null, null);
		
		int count = 0;
		if (cursor.moveToFirst())
			count = cursor.getInt(0);
		cursor.close();
		
		return count;
	}
	
	/**
	 * д��¼��
	 * @param context context, 
	 * @param value ��¼��
	 * @return ��¼URI��
	 */
	public static Uri insert(Context context, InksValue value) {
		return context.getContentResolver().insert(Inks.CONTENT_URI, ConvertToContentValues(value));
	}
	
	/**
	 * ���¼�¼��
	 * @param context context, 
	 * @param value ��¼��
	 * @return ��¼��
	 */
	public static int update(Context context, InksValue value){
		Uri uri = Uri.withAppendedPath(Inks.CONTENT_URI, value._id.toString());
		
		return context.getContentResolver().update(uri, ConvertToContentValues(value), 
				null, null);
	}
	
	/**
	 * ɾ���¼��
	 * @param context context, 
	 * @param value ��¼��
	 * @return ��¼��
	 */
	public static int delete(Context context, InksValue key){
		return context.getContentResolver().delete(Inks.CONTENT_URI, getWhereClause(key), null);
	}
	
	// ------------------------------------------------------------ Private methods
	
	/**
	 * 
	 */
	private static String getWhereClause(InksValue key) {
		StringBuffer clause = new StringBuffer();
		
		if (key._id != null)
			append(clause, String.format("%s=%d", Inks._ID, key._id));
		
		if (key.batch != null)
			append(clause, String.format("%s=%d", Inks.BATCH, key.batch));
		
		if (key.pageNumber != null)
			append(clause, String.format("%s=%d", Inks.PAGE_NUMBER, key.pageNumber));
		
		return (clause.length() > 0 )? clause.toString() : null;
	}
	/**
	 * ��Ӳ�ѯ������
	 */
	private static void append(StringBuffer clause, String selection) {
		if (clause.length() > 0 ) clause.append(" and ");
		
		clause.append(selection);
	}
	
	/**
	 * ��cursor�л�ȡ��ݡ�
	 * @param cursor cursor��
	 * @return ��¼��
	 */
	private static InksValue getData(Cursor cursor) {
		InksValue value = new InksValue();
		
		int colIndex = cursor.getColumnIndex(Inks._ID);
		if (colIndex != -1)
			value._id = cursor.getInt(colIndex);
		
		colIndex = cursor.getColumnIndex(Inks.BATCH);
		if (colIndex != -1)
			value.batch = cursor.getInt(colIndex);
		
		colIndex = cursor.getColumnIndex(Inks.PAGE_NUMBER);
		if (colIndex != -1)
			value.pageNumber = cursor.getInt(colIndex);
		
		colIndex = cursor.getColumnIndex(Inks.CONTENT);
		if (colIndex != -1)
			value.content = cursor.getBlob(colIndex);
		
		colIndex = cursor.getColumnIndex(Inks.IMAGE_BUFFER);
		if (colIndex != -1)
			value.imageBuffer = cursor.getBlob(colIndex);
		
		colIndex = cursor.getColumnIndex(Inks.TEXT_BUFFER);
		if (colIndex != -1)
			value.textBuffer = cursor.getString(colIndex);
		
		colIndex = cursor.getColumnIndex(Inks.BATCH_COUNT);
		if (colIndex != -1)
			value.batchCount = cursor.getInt(colIndex);
		colIndex = cursor.getColumnIndex(Inks.Thumbnail);
		if(colIndex!=-1){
			value.thumbnail=cursor.getBlob(colIndex);
		}
		return value;
	}
	
	/**
	 * ת����
	 */
	private static ContentValues ConvertToContentValues(InksValue value) {
		ContentValues cv = new ContentValues();
		
		cv.put(Inks._ID, value._id);
		cv.put(Inks.BATCH, value.batch);
		cv.put(Inks.PAGE_NUMBER, value.pageNumber);
		cv.put(Inks.CONTENT, value.content);
		cv.put(Inks.IMAGE_BUFFER, value.imageBuffer);
		cv.put(Inks.TEXT_BUFFER, value.textBuffer);
		
		return cv;
	}
}
